
import { _decorator, Node, MeshRenderer, Material, Color, Mesh, TERRAIN_HEIGHT_BASE, utils, pipeline, Vec4 } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import GameConfig from '../../Script/GameSpecial/GameConfig';
const { ccclass, property } = _decorator;

@ccclass('AtkScopeFan')
export class AtkScopeFan extends yyComponent {

    public init() {
        this.initMesh();
    }

    /*******************************管理对象*******************************/
    /**设置攻击范围颜色 */
    public setColor(c: Color) {
        let r = c.r / 255;
        let g = c.g / 255;
        let b = c.b / 255;
        let a = c.a / 255;
        this.meshData.colors = [
            r, g, b, a,
            r, g, b, a,
            r, g, b, a,
            r, g, b, a,
        ];
        utils.createMesh(this.meshData, this.mesh);
        this.meshRender.mesh = this.mesh;
    }
    /**设置半径 */
    public setRadius(r: number) {
        this.meshData.positions = [
            -r, 0, -r,
            -r, 0, r,
            r, 0, r,
            r, 0, -r,
        ];
        utils.createMesh(this.meshData, this.mesh);
        this.meshRender.mesh = this.mesh;
    }
    /**设置弧度范围 */
    public setHalfRadian(r: number) {
        let pass = this.meshRender.getMaterialInstance(0).passes[0];
        let handle = pass.getHandle("scope");
        let data = new Vec4();
        pass.getUniform(handle, data);
        data.x = r * 2;
        pass.setUniform(handle, data);
    }

    /*******************************管理对象*******************************/
    //#region 模型材质
    @property(MeshRenderer)
    protected meshRender: MeshRenderer = null;
    protected meshData: { positions: number[], uvs: number[], indices: number[], colors: number[] } = null;
    protected mesh: Mesh = null;
    protected initMesh() {
        this.createMesh();
    }
    protected createMesh() {
        let r = GameConfig.GameRule.atkScopeFan.radius;
        this.meshData = {
            positions: [
                -r, 0, -r,
                -r, 0, r,
                r, 0, r,
                r, 0, -r,
            ],
            indices: [
                0, 1, 2,
                0, 2, 3
            ],
            uvs: [
                0, 0,
                0, 1,
                1, 1,
                1, 0,
            ],
            colors: [
                1, 1, 1, 1,
                1, 1, 1, 1,
                1, 1, 1, 1,
                1, 1, 1, 1,
            ],
        };
        this.mesh = new Mesh();
        this.mesh = utils.createMesh(this.meshData);
        this.meshRender.mesh = this.mesh;
    }
    //#endregion

}